package com.fancy.learncenter.ui.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.AudioBean;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.ServiceBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.ServiceAdapter;
import com.fancy.learncenter.ui.callback.ServiceImp;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.PermissionHelper;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 我的客服
 */
public class MyServiceActivity extends BaseActivity {

    @Bind(R.id.service_recycle)
    RecyclerView serviceRecycle;
    ServiceAdapter adapter;
    List<ServiceBean.ResultBean> beanList;
    PermissionHelper mHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_service, Status.STATUS);
        ButterKnife.bind(this);
        initView();
        getData();
    }

    private void initView() {
        initToolbar("我的客服");
        beanList = new ArrayList<>();
        mHelper = new PermissionHelper(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        serviceRecycle.setLayoutManager(layoutManager);
        adapter = new ServiceAdapter(this, beanList, new ServiceImp() {
            @Override
            public void onClick(final String phone) {

                if (!mHelper.hasPermissions(this, Manifest.permission.CALL_PHONE)) {
                    mHelper.requestPermissions("请授予环球少年文件获取权限！",
                            new PermissionHelper.PermissionListener() {
                                @Override
                                public void doAfterGrand(String... permission) {
                                    String action = Intent.ACTION_CALL;
                                    Intent intent = new Intent(action, Uri.parse("tel:" + phone));
                                    if (ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                        // TODO: Consider calling
                                        //    ActivityCompat#requestPermissions
                                        // here to request the missing permissions, and then overriding
                                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                        //                                          int[] grantResults)
                                        // to handle the case where the user grants the permission. See the documentation
                                        // for ActivityCompat#requestPermissions for more details.
                                        return;
                                    }
                                    startActivity(intent);
                                }

                                @Override
                                public void doAfterDenied(final String... permission) {
                                    LogUtil.MyLog("mHelper", "========拒绝=======" + permission.toString());
                                    mHelper.showMessageOKCancel(getResources().getString(R.string.recode_permission), new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            mHelper.executePermissionsRequest(getBaseContext(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, mHelper.REQUEST_PERMISSION_CODE);
                                        }
                                    });
                                }
                            }, Manifest.permission.READ_EXTERNAL_STORAGE);
                } else {
                    Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+phone));
                    startActivity(intent);
                }
            }
        });
        serviceRecycle.setAdapter(adapter);

    }

    private void getData(){

        HttpMehtod.getInstance().getServiceInfo(new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> response) {

                BaseBean<ServiceBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(response),ServiceBean.class);
                CustomNetCallBack customNetCallBack = new CustomNetCallBack(getContext(),baseBean);
                customNetCallBack.setCallBack(new CustomNetCallBack.NetCallBack<ServiceBean>() {
                    @Override
                    public void success(ServiceBean dataBean) {

                        beanList.addAll(dataBean.getResult());
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();

                adapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        mHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
        mHelper.handleRequestPermissionsResult(requestCode,permissions,grantResults);
    }
}
